# 中间件
import json

from django.utils.deprecation import MiddlewareMixin
from django.shortcuts import HttpResponse

class AuthMiddleware(MiddlewareMixin):
    def process_request(selfs, request):
        # 排除不需要登录的接口
        if request.path_info in ['/x1/user/create', '/x1/login','/x1/image/code','/x1/login/out', '/x1/open/echarts']:
            return
        # 1.读取用户session信息，如果能读取到，说明已经登录过，可以继续往下走
        info_dirct = request.session.get("info")
        if info_dirct:
            return
        else:
            # 没有登录过返回请登录
            return HttpResponse(json.dumps({
                'code': 401,
                'msg': '登录失效, 请重新登录'
             }))
